﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obj;
using BLL;

public partial class AreaRestrita_Permissao : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ValidarUsuario();
        if (!IsPostBack)
        {
            List<objPermissao> lista = new List<objPermissao>();
            BLLPermissao regra = new BLLPermissao();
            lista = regra.lstPermissao();
            gdvPermissao.DataSource = lista;
            gdvPermissao.DataBind();
        }
    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
        Response.Redirect("PermissaoForm.aspx", true);
    }
    protected void btnFiltrar_Click(object sender, EventArgs e)
    {
        objPermissao Permissao = new objPermissao(txtPermissao.Text);
        BLLPermissao Regra = new BLLPermissao();

        List<objPermissao> Permissoes = Regra.lstPermissao(Permissao);

        gdvPermissao.DataSource = Permissoes;
        gdvPermissao.DataBind();
    }
    protected void gdvPermissao_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            objPermissao Permissao = (objPermissao)e.Row.DataItem;
            ImageButton imgEditar = (ImageButton)e.Row.FindControl("imgEditar");
            imgEditar.CommandArgument = Permissao.PermissaoID.ToString();

            ImageButton imgExcluir = (ImageButton)e.Row.FindControl("imgExcluir");
            imgExcluir.CommandArgument = Permissao.PermissaoID.ToString();
        }
    }
    protected void gdvPermissao_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Excluir")
        {
            BLLPermissao regra = new BLLPermissao();
            objPermissao Permissao = new objPermissao(Convert.ToInt32(e.CommandArgument));
            regra.Save(Permissao, action.DELETE);

            ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "ToggleScript",
            "alert('Permissão excluída com sucesso!');window.location.href='Permissao.aspx'",
            true);
        }
        else if (e.CommandName == "Editar")
        {
            Session["PermissaoID"] = e.CommandArgument;
            Response.Redirect("PermissaoForm.aspx", true);
        }
    }
}